package model;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/*
 * Sends given Message to the given Destination,
 * receives a Reply and returns it to the Manager
 */

public class Handler implements Runnable {

	@Override
	public void run() {
		 String factoryName = "weblogic.jndi.TengahInitialContextFactory";
				 //"mycf";weblogic.jms.ConnectionFactory
         String destinationName = "myq";
         
         //!!
         String msg = "Hello!";
         
         
//         Hashtable ht = new Hashtable();
//         ht.put(Context.INITIAL_CONTEXT_FACTORY,
//                "weblogic.jndi.WLInitialContextFactory");
//         ht.put(Context.PROVIDER_URL,
//                "t3://localhost:7001");
         
         try {
			InitialContext initContext = new InitialContext();
			ConnectionFactory factory =
	                (ConnectionFactory) initContext.lookup(factoryName);
	        Destination destination = (Destination) initContext.lookup(destinationName);
	        initContext.close();
	        
	        Connection connection = factory.createConnection();
            Session session =
                connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer sender = session.createProducer(destination);
            
            
	        TextMessage message = session.createTextMessage(msg);
            sender.send(message);
//        	MessageConsumer receiver = session.createConsumer(destination);
//        	receiver.receive();
	        
	        
	        
	        //connection.close();
	            
			
			
			
		} catch (NamingException e) {
			//TODO
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
